package com.lxmes.gs.controller;

import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.createTaskVO.*;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.gs.domain.CreateTask;
import com.lxmes.gs.service.ICreateTaskService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 创建任务Controller
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Api(tags = "创建任务")    
@RestController
@RequestMapping("/gs/createTask")
public class CreateTaskController extends BaseController{
    @Autowired
    private ICreateTaskService createTaskService;

    /**
     * 查询创建任务列表
     */
    @ApiOperation("查询创建任务列表")
    @PreAuthorize("@ss.hasPermi('gs:createTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(CreateTaskSearchVO createTask){
        startPage();
        List<CreateTask> list = createTaskService.selectCreateTaskList(BeanUtil.copyProperties(createTask,CreateTask.class));
        return getDataTable(Convert.convert(new TypeReference<List<CreateTaskResultVO>>() {},list));
}

    /**
     * 导出创建任务列表
     */
    @ApiOperation("导出创建任务列表")
    @PreAuthorize("@ss.hasPermi('gs:createTask:export')")
    @Log(title = "创建任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreateTask createTask){
        List<CreateTask> list = createTaskService.selectCreateTaskList(createTask);
        List<CreateTaskExportVO> exportList = Convert.convert(new TypeReference<List<CreateTaskExportVO>>() {}, list);
        ExcelUtil<CreateTaskExportVO> util = new ExcelUtil<CreateTaskExportVO>(CreateTaskExportVO.class);
        util.exportExcel(response, exportList, "创建任务数据");
    }

    /**
     * 获取创建任务详细信息
     */
    @ApiOperation("获取创建任务详细信息")
    @PreAuthorize("@ss.hasPermi('gs:createTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(createTaskService.selectCreateTaskById(id));
    }

    /**
     * 新增创建任务
     */
    @ApiOperation("新增创建任务")
    @PreAuthorize("@ss.hasPermi('gs:createTask:add')")
    @Log(title = "创建任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CreateTaskCreateVO createTask){
        return toAjax(createTaskService.insertCreateTask(BeanUtil.copyProperties(createTask,CreateTask.class)));
    }

    /**
     * 修改创建任务
     */
    @ApiOperation("修改创建任务")
    @PreAuthorize("@ss.hasPermi('gs:createTask:edit')")
    @Log(title = "创建任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CreateTaskUpdateVO createTask){
        return toAjax(createTaskService.updateCreateTask(BeanUtil.copyProperties(createTask,CreateTask.class)));
    }

    /**
     * 删除创建任务
     */
    @ApiOperation("删除创建任务")
    @PreAuthorize("@ss.hasPermi('gs:createTask:remove')")
    @Log(title = "创建任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(createTaskService.deleteCreateTaskByIds(ids));
    }
    /**
     * 创建任务审核
     */
    @ApiOperation("创建任务审核")
    @PreAuthorize("@ss.hasPermi('gs:createTask:audit')")
    @Log(title = "创建任务审核")
	@PostMapping("/auditCreateTask/{ids}")
    public AjaxResult auditCreateTask(@PathVariable Long[] ids){
        return toAjax(createTaskService.auditCreateTask(ids));
    }
    /**
     * 导入创建任务
     */
    @Log(title = "创建任务", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<CreateTaskImportVO> util = new ExcelUtil<>(CreateTaskImportVO.class);
        List<CreateTaskImportVO> createTaskList = util.importExcel(file.getInputStream());
        String message = createTaskService.imporIteminfo(Convert.convert(new TypeReference<List<CreateTask>>() {},createTaskList), updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        ExcelUtil<CreateTaskImportVO> util = new ExcelUtil<>(CreateTaskImportVO.class);
        util.importTemplateExcel(response,"创建任务");
    }
}
